#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int x=0;bool f=0;char ch=getchar();
	while(ch<'0'||ch>'9')f^=(ch=='-'),ch=getchar();
	while('0'<=ch&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int Maxn=1e5+5,inf=1e18;
int n,m,q;
int c[Maxn];
int a[Maxn][10];
int f[Maxn],last[6];
int sum[Maxn][6];
signed main(){
//	freopen("paint.in","r",stdin);
//	freopen("paint1.out","w",stdout);
	n=read();m=read();q=read();
	for(int i=1;i<=n;i++)c[i]=read();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++)a[i][j]=read(),sum[i][j]=sum[i-1][j]+a[i][j];
	}
	for(int i=1;i<=n;i++){
		f[i]=f[i-1]+a[i][c[i]];
		if(last[c[i]])f[i]=max(f[i],f[last[c[i]]]+sum[i][c[i]]-sum[last[c[i]]][c[i]]);
		last[c[i]]=i;
		printf("f[%d]=%d\n",i,f[i]);
	}printf("%lld\n",f[n]);
	return 0;
}
/*
10 3 0
3 2 2 3 3 2 3 1 1 3 1 6 10 
10 2 2 
7 10 5 
5 8 1 
5 10 4 
7 3 6 
6 1 4 
6 10 7 
1 6 5 
2 1 6 

*/
